package com.linesum.code.config;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.google.common.collect.Maps;

/**
 * 全局异常处理
 * @author chenzf
 * @date 2017年3月18日
 */
@Component
public class GlobalExceptionHandle implements HandlerExceptionResolver, Ordered {
	private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandle.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		LOGGER.error("捕获到全局异常,请求链接:{}", request.getRequestURI(), ex);
		Map<String, Object> map = Maps.newHashMap();
		map.put("result", false);
		map.put("message", "请求错误");
		return new ModelAndView(new MappingJackson2JsonView(), map);
	}

	@Override
	public int getOrder() {
		return 0;
	}
}
